﻿
var ns, toast, actionsheet, WXPhotoPicker;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,
        form: {
            ID: 0,
            RoomID: 0,
            ServiceFullName: '',//报修位置
            AddCallPhone: '',//报修人联系电话
            ServiceContent: '',//报修内容
            ServiceType: 'bsbx',
            AddCustomerName: '',//报修人
            Type: 2,
            PublicProjectID: 0,
            LevelID:0,
            LevelName:'普通',
            TagID:0,
            TagName:'',
        },
        IsLevelOpen:false,
        LevelList: [
            { ID: 0, Name: '普通', },
            { ID: 1, Name: '紧急', },
        ],
        IsTagOpen:false,
        TagList: [],
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
        filelist:[],
        task_imgList:[],
        DelImgIDs:[],
    },
    methods: {
        get_Tag(){
            var that = this;
            var options = {};    
            options.action = "getservicetaglist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                   that.TagList=data.list;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_data: function() {
            var that = this;
            var options = {};
            options.ID = that.ID;    
            options.action = "getnewservicedetail";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.form.AddCustomerName= data.form.customername;
                    that.form.AddCallPhone= data.form.customerphone;
                    that.form.LevelID= data.form.LevelID;
                    that.form.LevelName= data.form.LevelIDDesc;
                    that.form.TagID= data.form.TagID;
                    that.form.TagName=data.form.TagIDDesc;
                    that.form.ServiceContent= data.form.content;
                    that.task_imgList = data.form.imagelist;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_save: function (item) {
            var that = this;
            if (!that.form.AddCustomerName) {
                api.toast({
                    msg: '请填写报修人',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.ServiceContent == "") {
                api.toast({
                    msg: '请填写报事内容',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var options={
                action:'customerserviceedit',
                ID:that.ID,
                CustomerName:that.form.AddCustomerName,
                PhoneNumber:that.form.AddCallPhone,
                UrgencyStatus:that.form.LevelID,
                TaskTypeID:that.form.TagID,
                Content:that.form.ServiceContent,
                DelImgIDs:JSON.stringify(that.DelImgIDs),
            };
            toast.loading({
                title: "提交中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function (succeed, data, err) {
                toast.hide();
                that.is_searching = false;
                if (succeed) {
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                     //刷新详情界面
                     api.sendEvent({
                        name: 'updata_task_con',
                    });
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: that.filelist
                }
            });
        },
        choose_picture: function () {
            var that = this;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.do_imageFilter(ret.data);
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.do_imageFilter(item.path);
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        //图片压缩
        do_imageFilter:function(path){
            var that=this;
            var imgName = Date.now() + '.jpg';
            var imgFullName = 'fs://photo/' + imgName;
            var imageFilter = api.require('imageFilter');
            imageFilter.compress({
                img: path,
                isClarityimg:false,
                quality: 0.2,
                scale:0.5,
                save: {
                    album: true,
                    imgPath: 'fs://photo/',
                    imgName: imgName
                },
            },function( ret, err ){      
                if( ret.status ){
                    that.filelist.push(imgFullName);
                }else{
                    alert( JSON.stringify( err ) );
                }
            });
        },
        do_choose_type: function () {
            var that = this;
            ns.openWin('neibubaoshi_typelist', '工单类型选择', {
                gongdanType: 1,
            });
        },
        remove_img: function (index) {
            var that = this;
            that.imglist.splice(index, 1);
            that.filelist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        remove_img_edit: function (ID) {
            var that = this;
            for (var i = 0; i < that.task_imgList.length; i++) {
                var item = that.task_imgList[i];
                if (item.ID == ID) {
                    that.DelImgIDs.push(ID);//保存需要删除的图片ID
                    that.task_imgList.splice(i, 1);
                }
            }
        },
        do_viewimages_edit: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.task_imgList.length; i++) {
                imageUrls.push(that.task_imgList[i].imgurl);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        //选择紧急程度
        do_chooseLevel:function(){
            var that = this;
            that.IsLevelOpen=true;
        },
        doCloseLevel:function(){
            var that = this;
            that.IsLevelOpen=false;
        },
        doSelectLevel:function(item){
            var that = this;
            that.IsLevelOpen=false;
            that.form.LevelID = item.ID;
            that.form.LevelName = item.Name;   
        },   
         //选择任务标签
         do_chooseTag:function(){
            var that = this;
            that.IsTagOpen=true;
        },
        doCloseTag:function(){
            var that = this;
            that.IsTagOpen=false;
        },
        doSelectTag:function(item){
            var that = this;
            that.IsTagOpen=false;
            that.form.TagID = item.ID;
            that.form.TagName = item.TaskName;   
        },   
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.ID = api.pageParam.id || 0;
    app.get_Tag();//获取任务标签
    app.get_data();
};
